import React, { useEffect, useState } from 'react'
import { Button, Descriptions, PageHeader, message } from 'antd';
import moment from 'moment'
import { HeartTwoTone } from '@ant-design/icons';
import axios from 'axios';
export default function Detail(props) {
    const [previewInfo, setpreviewInfo] = useState(null)
    useEffect(() => {
        axios.get(`/news/${props.match.params.id}?_expand=category&_expand=role`).then(res => {
            setpreviewInfo({
                ...res.data,
                view: res.data.view++
            })
            return res.data
        }).then((res) => {
            axios.patch(`/news/${props.match.params.id}`, { view: res.view++ })
        })
    }, [props.match.params.id])
    const starAdd = () => {
        setpreviewInfo({
            ...previewInfo,
            star: previewInfo.star++
        })

        axios.patch(`/news/${props.match.params.id}`, { star: previewInfo.star++ })
        message.success('点赞成功')
    }
    return (
        <div style={{ margin: '0 10px' }}>
            {
                previewInfo && <div>
                    <PageHeader
                        ghost={false}
                        onBack={() => window.history.back()}
                        title={previewInfo?.title}
                        subTitle={<div>{previewInfo.category.title} <HeartTwoTone onClick={() => starAdd()} twoToneColor="#eb2f96" /></div>}
                    >
                        <Descriptions size="small" column={3}>
                            <Descriptions.Item label="创建者：">{previewInfo.author}</Descriptions.Item>

                            <Descriptions.Item label="发布时间：">{previewInfo.publishTime ? moment(previewInfo.publishTime).format('YYYY/MM/DD HH:MM:SS') : '-'}</Descriptions.Item>
                            <Descriptions.Item label="区域：">{previewInfo.region}</Descriptions.Item>

                            <Descriptions.Item label="访问数量：">
                                {previewInfo.view}
                            </Descriptions.Item>
                            <Descriptions.Item label="点赞数量：">
                                {previewInfo.star}
                            </Descriptions.Item>
                            <Descriptions.Item label="评论数量：">
                                0
                            </Descriptions.Item>
                        </Descriptions>
                    </PageHeader>
                    <div dangerouslySetInnerHTML={{
                        __html: previewInfo.content
                    }}
                        style={{ borderTop: '1px solid gray' }}
                    >
                    </div>
                </div>
            }
        </div>
    )
}
